import datetime
import akshare as ak
import csv
import time
#print(ak.__version__)


#stock_zh_a_minute_df = ak.stock_zh_a_minute(symbol='sh000300', period='60')
#print(stock_zh_a_minute_df)

#stock_zh = ak.stock_zh_a_hist(symbol="000001", period="daily", start_date="20240501", end_date='20250522', adjust="")
#print(stock_zh)

buycode = []

def isbuy(stockcode):
    #print(stockcode)
    code = "symbol=\""+str(stockcode)+"\""
    #print(code)
    stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol=f"{stockcode}", start_date="2024-05-01 09:00:00", end_date="2024-05-29 12:00:00", period="60", adjust="")
    for df in stock_zh_a_hist_min_em_df.iterrows():
       print(df)
#print(stock_zh_a_hist_min_em_df)
    count=len(stock_zh_a_hist_min_em_df)
    print('数据总量:'+str(count))

    i=1
    ma5=0
    ma51=0
    while i<=5:
        #print(stock_zh_a_hist_min_em_df.loc[count-i].at['收盘'])
        ma5=ma5+stock_zh_a_hist_min_em_df.loc[count-i].at['收盘']
        ma51=ma51+stock_zh_a_hist_min_em_df.loc[count-i-1].at['收盘']
        i=i+1
    ma5=ma5/5
    ma51=ma51/5
    print('5均:'+str(ma5))
    print(ma51)

    i=1
    ma13=0
    ma131=0
    while i<=13:
        #print(stock_zh_a_hist_min_em_df.loc[count-i].at['收盘'])
        ma13=ma13+stock_zh_a_hist_min_em_df.loc[count-i].at['收盘']
        ma131=ma131+stock_zh_a_hist_min_em_df.loc[count-i-1].at['收盘']
        i=i+1
    ma13=ma13/13
    print('13均:'+str(ma13))
    ma131=ma131/13
    print(ma131)

    i=1
    ma60=0
    ma601=0
    while i<=60:
        #print(stock_zh_a_hist_min_em_df.loc[count-i].at['收盘'])
        ma60=ma60+stock_zh_a_hist_min_em_df.loc[count-i].at['收盘']
        ma601=ma601+stock_zh_a_hist_min_em_df.loc[count-i-1].at['收盘']
        i=i+1   
    ma60=ma60/60    
    print('60均:'+str(ma60))
    ma601=ma601/60    
    print(ma601)

    lastc=stock_zh_a_hist_min_em_df.loc[count-1].at['收盘']
    print('收盘0:'+str(lastc))
    lastc1=stock_zh_a_hist_min_em_df.loc[count-2].at['收盘']
    print('收盘1:'+str(lastc1))
    lasto=stock_zh_a_hist_min_em_df.loc[count-1].at['开盘']
    print('开盘:'+str(lasto))
    #成交量
    lastv0=stock_zh_a_hist_min_em_df.loc[count-1].at['成交量']
    lastv1=stock_zh_a_hist_min_em_df.loc[count-2].at['成交量']
    print('成交量0:'+str(lastv0))
    print('成交量1:'+str(lastv1))
    
    lasttime=stock_zh_a_hist_min_em_df.loc[count-1].at['时间']
    print('时间:'+str(lasttime))
    #
    if ma5>ma13  and ma5>ma51 and lastc>lasto and lastv0>lastv1:
        print('基础条件:'+stockcode)
        if lastc>ma5 and ma5>ma60 and ma51<ma601:
        #5线上穿60线    
            print('5线上穿60线:'+stockcode)

        #buycode=buycode+":"+stockcode
            buycode.append(stockcode)

        if ma5>ma60 and ma51>ma601 and ma13>ma60  and  ma131>ma601 and  ma51<ma131:
        #5线上穿13线，在60线上    
            print('5线上穿13线:'+stockcode)

        #buycode=buycode+":"+stockcode
            buycode.append(stockcode)
        
        if lastc>ma60 and lastc1<ma601 and ma13>ma131:
        #价上穿60线    
            print('价上穿60:'+stockcode)
            buycode.append(stockcode)
        #buycode=buycode+":"+stockcode    
    #for kp in stock_zh_a_hist_min_em_df[['时间','开盘','收盘']].iterrows():
    #  print(kp)
    if ma5>ma60 and ma5>ma51 and ma13>ma60  and  ma13>ma131 and  ma60>ma601 and lastc>ma5 and lastc>ma13:
        print('均线多头:'+stockcode)
        buycode.append(stockcode)
    #for sp in stock_zh_a_hist_min_em_df.iterrows():
    #  print(sp)  

with open('D:\\akshare\\stock.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
        isbuy(row[0])
        time.sleep(0.1)

# 
print("结果:"+str(datetime.datetime.now()))
print(buycode)